CREATE OR REPLACE TYPE PL_COUCH_REV AS OBJECT (
  -- ---------------------------------------------------------------------------
  -- Copyright (C) 2009/2010 Stefan Neudorfer / sttn (s.neudorfer@neudorfer.de)
  --
  -- Licensed under the Apache License, Version 2.0 (the "License");
  -- you may not use this file except in compliance with the License.
  -- You may obtain a copy of the License at
  -- 
  -- http://www.apache.org/licenses/LICENSE-2.0
  -- 
  -- Unless required by applicable law or agreed to in writing, software
  -- distributed under the License is distributed on an "AS IS" BASIS,
  -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  -- See the License for the specific language governing permissions and
  -- limitations under the License.
  -- 
  -- ---------------------------------------------------------------------------
  -- Remarks:
  -- Create ACL's before using this packages, also for the owner from this package.
  -- (see PL_COUCH_README.txt)
  -- and create grants to the owner from this package:
  -- grant CREATE PUBLIC SYNONYM to cassis;
  -- grant drop PUBLIC SYNONYM to cassis;
  --
  -- -----------------------------------------------------------------------
  -- STTN      | 2009.12.16 | Created
  --           |            |
  --           |            |
  --           |            |
  --           |            |
  --           |            |
  --
  -- database
  database VARCHAR2(64),
  -- document Id
  doc_id VARCHAR2(64),
  -- Revision-ID-Table
  revision_table PL_COUCH_REV_ARRAY,
  --
  -- ------------------------------------------
  -- Init 
  CONSTRUCTOR FUNCTION PL_COUCH_REV
              RETURN SELF AS RESULT,
  --
  -- ------------------------------------------
  -- Init with initial parameters
  -- database   - Name from the database
  -- doc_id     - ident from the document
  CONSTRUCTOR FUNCTION PL_COUCH_REV (database    VARCHAR2
                                    ,doc_id      VARCHAR2)
              RETURN SELF AS RESULT,
  --
  -- ---------------------------------------------------
  -- Get name from the database
  MEMBER FUNCTION GET_DATABASE RETURN VARCHAR2,
  --
  -- ---------------------------------------------------
  -- Get document id
  MEMBER FUNCTION GET_DOC_ID RETURN VARCHAR2,
  --
  -- ---------------------------------------------------
  -- Get count revisions
  MEMBER FUNCTION GET_COUNT_REVISION RETURN INTEGER,
  --
  -- ---------------------------------------------------
  -- Get revision id, indexed by integer value
  MEMBER FUNCTION GET_REVISION_ID(NR INTEGER) RETURN VARCHAR2,
  --
  -- ---------------------------------------------------
  -- Get revision state, indexed by integer value
  MEMBER FUNCTION GET_REVISION_STATUS(NR INTEGER) RETURN VARCHAR2,
  --
  -- ---------------------------------------------------
  -- Set database
  MEMBER PROCEDURE SET_DATABASE(database VARCHAR2),
  --
  -- ---------------------------------------------------
  -- Set document id
  MEMBER PROCEDURE SET_DOC_ID(doc_id VARCHAR2),
  --
  -- ---------------------------------------------------
  -- Add revision and state
  MEMBER PROCEDURE ADD_REVISION(revision_id  VARCHAR2
                               ,status       VARCHAR2)
  --
);

/

show err;

CREATE OR REPLACE TYPE BODY PL_COUCH_REV AS
  -- ---------------------------------------------------------------------------
  -- Copyright (C) 2009/2010 Stefan Neudorfer / sttn (s.neudorfer@neudorfer.de)
  --
  -- Licensed under the Apache License, Version 2.0 (the "License");
  -- you may not use this file except in compliance with the License.
  -- You may obtain a copy of the License at
  -- 
  -- http://www.apache.org/licenses/LICENSE-2.0
  -- 
  -- Unless required by applicable law or agreed to in writing, software
  -- distributed under the License is distributed on an "AS IS" BASIS,
  -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  -- See the License for the specific language governing permissions and
  -- limitations under the License.
  --
  -- ------------------------------------------
  -- Init 
  CONSTRUCTOR FUNCTION PL_COUCH_REV RETURN SELF AS RESULT
  AS
  BEGIN
     self.revision_table := PL_COUCH_REV_ARRAY();
  END;
  --
  -- ------------------------------------------
  -- Init with initial parameters
  -- database   - Name from the database
  -- doc_id     - ident from the document
  CONSTRUCTOR FUNCTION PL_COUCH_REV (database    VARCHAR2
                                   ,doc_id      VARCHAR2)
              RETURN SELF AS RESULT
  IS
  BEGIN
     self.revision_table := PL_COUCH_REV_ARRAY();
     set_Database(database);
     set_Doc_id(doc_id);
     RETURN;
  END;
  --
  -- ---------------------------------------------------
  -- Get name from the database
  MEMBER FUNCTION GET_DATABASE
         RETURN VARCHAR2
  IS
  BEGIN
    RETURN database;
  END GET_DATABASE;
  --
  -- ---------------------------------------------------
  -- Get document id
  MEMBER FUNCTION GET_DOC_ID
         RETURN VARCHAR2
  IS
  BEGIN
    RETURN doc_id;
  END GET_DOC_ID;
  --
  -- ---------------------------------------------------
  -- Get count revisions
  MEMBER FUNCTION GET_COUNT_REVISION RETURN INTEGER
  IS
  BEGIN
    RETURN revision_table.count;
  END;
  --
  -- ---------------------------------------------------
  -- Get revision id, indexed by integer value
  MEMBER FUNCTION GET_REVISION_ID(NR INTEGER) RETURN VARCHAR2
  IS
    oneRec PL_COUCH_REV_REC;
  BEGIN
    IF NR >= revision_table.count OR
       NR < 0 THEN
       RETURN NULL;
    END IF;
    oneRec := revision_table(NR);
    RETURN oneRec.revision_id;
  END;
  --
  -- ---------------------------------------------------
  -- Get revision state, indexed by integer value
  MEMBER FUNCTION GET_REVISION_STATUS(NR INTEGER) RETURN VARCHAR2
  IS
    oneRec PL_COUCH_REV_REC;
  BEGIN
    IF NR >= revision_table.count OR
       NR < 0 THEN
       RETURN NULL;
    END IF;
    oneRec := revision_table(NR);
    RETURN oneRec.status;
  END;
  --
  -- ---------------------------------------------------
  -- Set database
  MEMBER PROCEDURE SET_DATABASE(database VARCHAR2)
  IS
  BEGIN
     self.database := database;
  END SET_DATABASE;
  --
  -- ---------------------------------------------------
  -- Set document id
  MEMBER PROCEDURE SET_DOC_ID(doc_id VARCHAR2)
  IS
  BEGIN
     self.doc_id := doc_id;
  END SET_DOC_ID;
  --
  -- ---------------------------------------------------
  -- Add revision and state
  MEMBER PROCEDURE ADD_REVISION(revision_id  VARCHAR2
                               ,status       VARCHAR2)
  IS
  BEGIN
    revision_table.extend;
    revision_table(revision_table.count) := PL_COUCH_REV_REC(revision_id, status);
  END;
  --
END;
/ 

grant all on PL_COUCH_DOC TO PUBLIC;

SHOW ERR;
